import store from '../store/index.js'
let code
let userInfo
let newCode
let werunIv
let werunEncryptedData
let werunCode
// 获取用户信息
function userLogin(share_user_id,fromJin){
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success(res) {
				code = res.code
				uni.getUserInfo({
					provider: 'weixin',
					async success(res) {
						userInfo = res
						console.log(res.userInfo)
						store.commit('updataUserInfo',res.userInfo);
						uni.setStorageSync("userInfo",res.userInfo)
						await testLogin(share_user_id,fromJin)
						await newInfo()
						await login();
						return 1
						resolve()
					}
				})
			}
		})
	})
	
}
// 授权注册
function testLogin(share_user_id,fromJin){
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '授权登录中',
		});
		let self = this
		uni.request({
			url:store.state.URL + 'login/login/mini_program_authorization',
			data:{
				code: code,
				iv: userInfo.iv,
				encryptedData: userInfo.encryptedData,
				share_user_id: share_user_id,
				fromJin: fromJin
			},
			method:"POST",
			success(res) {
				uni.hideLoading();
				resolve()
			}
		})
	})
	
}
// 重新获取code
function newInfo(){
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: function (loginRes) {
				newCode = loginRes.code;
				// 获取用户信息
				uni.getUserInfo({
					provider: 'weixin',
					success: function (infoRes) {
						resolve()
					}
				});
			}
		});
	})
}
// 登录中
function login(){
	return new Promise((resolve, reject) => {
		let self = this
		uni.request({
			url:store.state.URL + 'login/login/mini_program_login',
			data:{
				code: newCode,
			},
			method:"POST",
			success(res) {
				if(res.data.status) {
					store.commit('updataToken',res.data.token);
					store.commit('updataUserId',res.data.wx_user.id);
					store.commit('updataPhone',res.data.wx_user.phone);
					store.commit('updataCanJin',res.data.wx_user.is_can_jin)
					// 将用户的id保存在本地存储
					uni.setStorageSync("userId",res.data.wx_user.id)
					uni.setStorageSync("phone",res.data.wx_user.phone)
					uni.showToast({
						title: '授权登录成功！',
						icon: 'none',
						duration: 2000
					})
				}
				console.log('用户登录后的数据',res.data)
				// if(res.data.flag == '1'){
				// 	console.log('登录成功并返回token')
				// 	// uni.switchTab({
				// 	// 	url:'pages/home/home.vue'
				// 	// })
				// 	resolve()
				// }else if(res.data.flag == '2'){
				// 	console.log('登录成功但没有token')
				// 	reject()
				// }else{
				// 	if(res.data.error_code == 'shop_status_false'){
				// 		uni.showToast({
				// 			title: res.data.msg,
				// 			duration: 1000,
				// 			icon: "none",
				// 		});
				// 	}else if(res.data.flag == 0){
				// 		console.log("用户未注册");
				// 		newInfo();
				// 	}
				// 	reject()
				// }
			}
		})
	})
	
}
// 获取微信步数
function userWerun(){
	uni.login({
		provider:"weixin",
		success(res) {
			werunCode = res.code
			uni.authorize({
			    scope: 'scope.werun',
			    success(res) {
					console.log('获取微信步数授权success',res)
					wx.getWeRunData({
						success(res) {
							console.log('获取步数数据success',res)
							werunIv = res.iv
							werunEncryptedData = res.encryptedData
							testWerun()
						},
						fail(err) {
							console.log('获取步数数据err',err)
						}
					})
			    },
				fail(err) {
					console.log('获取微信步数授权err',err)
				}
			})
		}
	})
}
function testWerun(){
	uni.request({
		url:store.state.URL + 'login/login/mini_program_get_steps',
		data:{
			code: werunCode,
			iv: werunIv,
			encryptedData: werunEncryptedData,
			token: store.getters.getToken
		},
		success(res) {
			console.log(res)
			if(res.data.status&&res.data.msg=='获取成功') {
				console.log('获取用户的步数成功',res)
				store.commit('updataCanStep',true)
			}else {
				store.commit('updataCanStep',false)
			}
		},
		fail(err) {
			console.log('获取用户的步数失败',err)
		}
	})
}
// 检查是否已授权
function testGet(){
	uni.getSetting({
		success(res) {
			console.log(res)
			if(!res.authSetting['scope.userInfo']){
				newInfo()
				uni.showToast({
					title:"请先登录",
					icon:"none",
					duration:2000
				})
			}else{
				console.log('11111111111111')
			}
		},
		fail(err) {
			console.log(err)
		}
	})
}
module.exports = {
	userLogin: userLogin,
	testGet: testGet,
	login: login,
	userWerun: userWerun
}